package dao;

import dbc.DBI;
import dbc.MyConn;
import vo.News;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class NewDaoImpl implements INewDao{
    public static void main(String [] args){
        try {
            System.out.println(new NewDaoImpl().getAllNew().size());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    @Override
    public List<News> getAllNew() throws SQLException {
        List<News> list1=new ArrayList<>();
        //打开数据库
        DBI dbi = new MyConn();
        Connection connection = dbi.openConn();
        //发送SQL语句
        PreparedStatement ps = connection.prepareStatement("select * from  news");
        ResultSet rs = ps.executeQuery();  //所有的记录已经放到虚拟表中了   虚拟表在内存中，要放入集合类对象中    //  ps.executeUpdate()
        while (rs.next()){
            News news = new News();
            news.setId(rs.getInt(1));
            news.setAuther(rs.getString(4));
            news.setContent(rs.getString(3));
            news.setTitle(rs.getString(2));
            news.setTime(rs.getString(5));

            list1.add(news);
        }
        //释放资源   关闭数据库
        dbi.closeAll(null,ps,rs);
        dbi.closeConn();

        return list1;
    }

    @Override
    public News getOnNewById(int id) throws SQLException {
        return null;
    }

    @Override
    public boolean deleteBtid(int id) throws SQLException {
        return false;
    }
}
